<template>
    <div class="iscInfoWrap">
        <el-form
            :inline="true"
            :model="searchParams"
            style="width: 100%"
            size="small"
            class="formInline"
        >
          <el-form-item label="汇总月份：" style="max-width: 340px">
            <el-date-picker type="month"  value-format="yyyy-MM" placeholder="选择日期" v-model="searchParams.month1"  :picker-options="pickerOptions" style="width: 100%;"></el-date-picker>
          </el-form-item>

          <el-button @click="getList" type="primary" size="small">查询汇总确认信息</el-button>
          <el-button @click="uploadIsc" icon="el-icon-upload" type="primary" size="small" v-if="hasAuth('VMS_ISC_INFORMATION_UPLOAD')">上传汇总确认信息</el-button>
          <el-button @click="cancelIsc" icon="el-icon-upload" type="primary"  size="small" v-if="hasAuth('VMS_ISC_INFORMATION_CANCEL')">取消汇总确认</el-button>

        </el-form>
        <div style="margin-right: 20px;color: #07b9b9;margin-bottom: 8px;">发票汇总信息：</div>
        <el-table
          :data="comTableData"
          border
          style="width: 100%;margin-bottom: 8px;">
        <el-table-column
            prop="name"
            label="汇总项目"
            width="180">
        </el-table-column>
        <el-table-column
            prop="comData"
            label="企业端"
            width="180">
        </el-table-column>
        <el-table-column
            prop="lqData"
            label="税局端">
        </el-table-column>
      </el-table>
      <div style="margin-bottom: 8px;">
        <span style="margin-right: 20px;color: #07b9b9">发票详情信息</span>
          <el-button @click="exportExecl" type="primary"  size="small">导出</el-button>
      </div>
      <tableList
          :data="listDetails"
          border
          maxHeight="500"
          :keySet="keySet"
          :autoHeight="true"
          :tableData="listDetails"
          :paginationShow="true"
          :pageNumber="pages.current"
          :pageSize="pages.size"
          :total="pages.total"
          @handleSizeChange="handleSizeChange"
          @handleCurrentChange="handleCurrentChange"
      >

      </tableList>
    </div>

</template>
  
  <script>

import tableList from "@/components/tableList/index.vue";
import mixinTable from "@/mixin/mtable";
import {cancelIsc, exportExecl,uploadIsc, pageInvoice, queryIscInfomationPage} from "@/api/invoiceSummryConfirm.js";

export default {
    name: 'invoiceSummryConfirm',
    components:{
      //
      tableList
    },
    mixins: [mixinTable],
    created(){
      //this.getList()

    },
    data() {
      return {
        loading: false,
        listDetails: [],
        vmsIscInformationComVo:{},//企业端-暂时没用到
        vmsIscInformationLqVo: {},//税局端-暂时没用到
        blueInvoiceCount: 0,//蓝字发票数量
        blueInvoiceAmount: 0,//蓝字发票金额
        blueInvoiceTax: 0,//蓝字发票税额
        redInvoiceCount: 0,//红字发票数量
        redInvoiceAmount: 0,//红字发票金额
        redInvoiceTax: 0,//红字发票税额
        lqblueInvoiceCount: 0,//乐企蓝字发票数量
        lqblueInvoiceAmount: 0,//乐企蓝字发票金额
        lqblueInvoiceTax: 0,//乐企蓝字发票税额
        lqredInvoiceCount: 0,//乐企红字发票数量
        lqredInvoiceAmount: 0,//乐企红字发票金额
        lqredInvoiceTax: 0,//乐企红字发票税额

        // ''
        keySet: [
          {
            label: '发票状态',
            englishName: 'fpzt',
            width: '200px'
          },
          {
            label: '发票号码',
            englishName: 'fphm',
            width: '200px'
          },
          {
            label: '发票类型',
            englishName: 'fppz',
            width: '100px'
          },
          {
            label: '购买方名称',
            englishName: 'gmfmc',
            width: '250px'
          },
          {
            label: '金额',
            englishName: 'hjje',
            width: '200px'
          },
          {
            label: '税额',
            englishName: 'hjse',
            width: '100px'
          },
          {
            label: '价税合计',
            englishName: 'jshj',

          },

        ],
        comTableData:[
          {
            name: '蓝字发票数量',
            comData: '0',
            lqData: '0'
          },
          {
            name: '蓝字发票金额',
            comData:'0.00',
            lqData: '0.00'
          },
          {
            name: '蓝字发票税额',
            comData: '0.00',
            lqData: '0.00'
          },
          {
            name: '红字发票数量',
            comData:'0',
            lqData: '0'
          },
          {
            name: '红字发票金额',
            comData: '0.00',
            lqData: '0.00'
          },
          {
            name: '红字发票税额',
            comData: '0.00',
            lqData: '0.00'
          }
        ],
        searchParams: {
          month1: this.getDefaultMonth()
        },
        pickerOptions: {
          disabledDate(time) {
            let curMonth = new Date().getMonth();
          let curYear = new Date().getFullYear();
          let nextMonth = new Date(curYear, curMonth + 1, 0); // 获取下下个月的日期，月份加1后使用0获取最后一天
          return time.getTime() > nextMonth.getTime(); // 禁止选择下下个月及以后的日期
          }
        },
      }
    },
    methods:{
      // disabledDate(date) {
      //     const today = new Date();
      //   const currentMonth = today.getMonth();
      //   const currentYear = today.getFullYear();
      //   const targetDate = new Date(currentYear, currentMonth, 1); // 获取当前月份的第一天
      //   return date > targetDate; // 禁用未来月份
      // },
      exportExecl() {
       exportExecl({
          month: this.searchParams.month1,
        })

      },

      /**
       *取消汇总确认
       */
      async cancelIsc(){
        this.loading = true
        let params ={
          month:this.searchParams.month1,
          lzfpsl: this.blueInvoiceCount,//蓝字发票数量
          lzfpje: this.blueInvoiceAmount,//蓝字发票金额
          lzfpse: this.blueInvoiceTax,//蓝字发票税额
          hzfpsl: this.redInvoiceCount,//红字发票数量
          hzfpje: this.redInvoiceAmount,//红字发票金额
          hzfpse: this.redInvoiceTax,//红字发票税额
        }
        this.$alert('是否调用接口取消汇总确认？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          showCancelButton: true,
          type: 'warning',
        }) .then(async () => {
          let {data, code, message} = await cancelIsc(params)
          this.loading = false
          if (code == 200) {
            this.$message({
              type: 'success',
              message: '取消汇总确认成功！',
            })
            //取消成功，乐企归零
            this.comTableData[0].lqData ="0"
            this.comTableData[1].lqData ="0.00"
            this.comTableData[2].lqData ="0.00"
            this.comTableData[3].lqData ="0"
            this.comTableData[4].lqData ="0.00"
            this.comTableData[5].lqData ="0.00"
          }
        }) .catch(() => {

            })

      },
      /**
       *上传汇总确认信息
       */

      async uploadIsc(){
        this.loading = true
        let params ={
          month:this.searchParams.month1,
          lzfpsl: this.blueInvoiceCount,//蓝字发票数量
          lzfpje: this.blueInvoiceAmount,//蓝字发票金额
          lzfpse: this.blueInvoiceTax,//蓝字发票税额
          hzfpsl: this.redInvoiceCount,//红字发票数量
          hzfpje: this.redInvoiceAmount,//红字发票金额
          hzfpse: this.redInvoiceTax,//红字发票税额
        }
        this.$alert('是否调用接口上传汇总确认信息？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          showCancelButton: true,
          type: 'warning',
        }) .then(async () => {
          let {data, code, message} = await uploadIsc(params)
          this.loading = false
          if (code == 200) {
            this.$message({
              type: 'success',
              message: '汇总确认成功！',
            })
            //todo 上传成功，刷新列表，将企业端金额写到税局端
            this.comTableData[0].lqData = this.comTableData[0].comData;
            this.comTableData[1].lqData = this.comTableData[1].comData;
            this.comTableData[2].lqData = this.comTableData[2].comData;
            this.comTableData[3].lqData  =this.comTableData[3].comData;
            this.comTableData[4].lqData  =this.comTableData[4].comData;
            this.comTableData[5].lqData = this.comTableData[5].comData;
          }
        }) .catch(() => {

        })

      },
      /**
       * 点击分页按钮查询当前页发票数据
       */
      handleCurrentChange(val){
        this.pages.current = val;
        this.getDetailPage()
      },
      handleSizeChange(val){
        this.pages.current = 1;
        this.pages.size = val;
        this.getDetailPage()
      },
      //获取分页数据，这里必须是异步，否则数据拿不到
      async getDetailPage(){
        let data = await pageInvoice({
          month: this.searchParams.month1,
          pageNo: this.pages.current,
          pageSize: this.pages.size,
        })
        console.log(data)
        if (data.code == 200) {
          data = data.data
          this.loading = false
          this.listDetails = data.records.map(item=>{
                                return {
                                    ...item
                                }
                            })
          this.pages.total = data.total
          this.pages.current = data.current
          this.pages.size = data.size
        } else {
          this.loading = false

        }
      },
      /**
       * 查询列表数据
       * @param params
       * @returns {Promise<void>}
       */

      async getList() {
        this.loading = true
        this.$alert('是否调用接口查询汇总信息？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          showCancelButton: true,
          type: 'warning',
        }) .then(async () => {
          let {data, code, message} = await queryIscInfomationPage({
            month: this.searchParams.month1,
            pageNo: this.pages.current,
            pageSize: this.pages.size,
          })
          if (code == 200) {

              this.comTableData[0].lqData = data.vmsIscInformationLqVo.blueInvoiceCount
              this.comTableData[1].lqData = data.vmsIscInformationLqVo.blueInvoiceAmount
              this.comTableData[2].lqData = data.vmsIscInformationLqVo.blueInvoiceTax
              this.comTableData[3].lqData  = data.vmsIscInformationLqVo.redInvoiceCount
              this.comTableData[4].lqData  = data.vmsIscInformationLqVo.redInvoiceAmount
              this.comTableData[5].lqData = data.vmsIscInformationLqVo.redInvoiceTax

          } else {
            //this.loading = false
            this.comTableData[0].lqData ="0"
            this.comTableData[1].lqData ="0.00"
            this.comTableData[2].lqData ="0.00"
            this.comTableData[3].lqData ="0"
            this.comTableData[4].lqData ="0.00"
            this.comTableData[5].lqData ="0.00"
            //return false;
          }
            this.loading = false
            this.comTableData[0].comData = this.turnRules(data.vmsIscInformationComVo.blueInvoiceCount)=="0.00"?"0":data.vmsIscInformationComVo.blueInvoiceCount
            this.comTableData[1].comData = this.turnRules(data.vmsIscInformationComVo.blueInvoiceAmount)
            this.comTableData[2].comData = this.turnRules(data.vmsIscInformationComVo.blueInvoiceTax)
            this.comTableData[3].comData = this.turnRules(data.vmsIscInformationComVo.redInvoiceCount)=="0.00"?"0":data.vmsIscInformationComVo.redInvoiceCount
            this.comTableData[4].comData = this.turnRules(data.vmsIscInformationComVo.redInvoiceAmount)
            this.comTableData[5].comData = this.turnRules(data.vmsIscInformationComVo.redInvoiceTax)
            this.blueInvoiceCount=data.vmsIscInformationComVo.blueInvoiceCount;//蓝字发票数量
            this.blueInvoiceAmount=this.comTableData[1].comData ;//蓝字发票金额
            this.blueInvoiceTax=this.comTableData[2].comData;//蓝字发票税额
            this.redInvoiceCount=data.vmsIscInformationComVo.redInvoiceCount;//红字发票数量
            this.redInvoiceAmount= this.comTableData[4].comData;//红字发票金额
            this.redInvoiceTax=this.comTableData[5].comData;//红字发票税额
            // this.listDetails = data.vmsIscInformationDetailVo.records
            this.listDetails = data.vmsIscInformationDetailVo.records.map(item=>{
              return {
                ...item
              }
            })
            this.pages.total = data.vmsIscInformationDetailVo.total
            this.pages.current = data.vmsIscInformationDetailVo.current
            this.pages.size = data.vmsIscInformationDetailVo.size

        }) .catch(() => {

        })


      },
      turnRules(val) {
        //将val值为空情况下，转化为0
        if (!val || val ==0 ) {
          return "0.00"
        } else {
          return val
        }
      },
      getDefaultMonth() {
        const currentDate = new Date();
        const lastMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() - 1);
        const year = lastMonth.getFullYear();
        const month = (lastMonth.getMonth() + 1).toString().padStart(2, '0');
        return `${year}-${month}`;
      }

    },

}
</script>
  <style lang = "scss" scoped>
  @import '@/style/variables.scss';
  .iscInfoWrap{
    overflow: auto;
    height: 100vh;
  }
  .el-row {
    margin-bottom: 30px;

  }
  .epsInfoWrap {
    box-sizing: border-box;
    .head {
      display: flex;
      height: 18px;
      align-items: center;
      margin-bottom: 4px;
      margin-top: 4px;
    }
  }

</style>
  